package command;

import java.net.InetAddress;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.*;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;

import command.Command;
import command.CommandException;
import db.CommonConnect;

import common.CommonEduSoap;
import common.CommonUtil;
import common.Crypto;
import common.EDUBOX_Baese64_Crypto;
import common.UserSession;
import db.QueryManager;
import db.DAO;

/**
 * Command 인터페이스를 implement 하여 구현된 command.
 */
public class CopyOfOpMainCommand extends Command {

	private String next;
	
	public CopyOfOpMainCommand(String next) {
		this.next=next;
	}

	/**
	 * 실제 요청을 받아 처리하는 메소드
	 * 요청을 받으면  요청을 처리하고 next 페이지를 돌려줌
	 * 
	 * @param req HttpServletRequest
	 * @throws CommandException
	 * @return next 이동할 페이지
	 */
	public String execute(HttpServletRequest req)
	throws CommandException {
		ArrayList list =  null;
		HashMap listMap = new HashMap();
		
		HashMap userMap = new HashMap();
		Cookie[] cookies=req.getCookies();
		Connection con = null;  
		try {
			HttpSession session = req.getSession(true);
			
			String uni_id = "";		
			
			String login_id=Crypto.EncodeBySType(req.getParameter("id"));
			String login_pwd=Crypto.EncodeBySType(req.getParameter("pwd"));
			
					
			
			logger.debug(login_id+":"+login_pwd);
			if (login_id!=null && login_pwd!=null){
				con = (new CommonConnect()).getConnection_ds1();
				con.setAutoCommit(false);
				String[] param=null;
				String queryStr = QueryManager.getQuery("login.query");
				param= new String[2];
				param[0] = login_id;
				param[1] = login_pwd;
				
				list = DAO.select(con,queryStr,param);
				if (list==null || list.size()==0){
					req.setAttribute("login_message","ID is not member or Password is wrong!");
					//throw new CommandException("ID is not member or Password is wrong!");
				}else{
					userMap =(HashMap)list.get(0);
					
					
					UserSession userSession = (UserSession) new UserSession( (Map) userMap );					
		            session.setAttribute( "user", userSession );
		            
		           
		            String user_ip   = req.getRemoteAddr();

		      
			        		        
		            String cross_id = Crypto.EncodeBySType2((String)userMap.get( "login_id" ),user_ip);
		            
		            uni_id = userSession.getUser_id();
		            
		            Cookie cook=new Cookie( "usid", cross_id);
		          

		            cook.setMaxAge(-1);		            
		            logger.debug(cook.getName()+":"+cook.getValue());
		            System.out.println("==================>>>>>cookie:"+cook.getName()+":"+cook.getValue());
		            req.setAttribute("LoginCook", cook);

				}
				
				//멀티 체크박스 입력 테스트 시작
				String poi_cd[] 	= req.getParameterValues("poi_cd");
				String kisu_id[] 	= req.getParameterValues("kisu_id");
				String ent_id[] 	= req.getParameterValues("ent_id");		
				String checkbox[] 	= req.getParameterValues("checkbox");
				
				String poi_cd1[] 	= new String[checkbox.length];
				String kisu_id1[] 	= new String[checkbox.length];
				String ent_id1[] 	= new String[checkbox.length];
				
				String[] param_1 = new String[5];			
				
				for(int i =0 ; i < checkbox.length; i++){
					
					poi_cd1[i] 		= poi_cd[Integer.parseInt(checkbox[i])];
					kisu_id1[i] 	= kisu_id[Integer.parseInt(checkbox[i])];
					ent_id1[i] 		= ent_id[Integer.parseInt(checkbox[i])];
					
					
					System.out.println("넘겨온값 =====>" + " poi_cd===>"+poi_cd[Integer.parseInt(checkbox[i])]+ " kisu_id===>" + kisu_id[Integer.parseInt(checkbox[i])]);
					System.out.println("checkbox["+i+"]==>"+checkbox[i]);
					
					param_1[0]=poi_cd1[i];
					param_1[1]="1";
					param_1[2]=ent_id1[i] ;
					param_1[3]=kisu_id1[i];
					param_1[4]="10.10.10.104";
					
					queryStr =  QueryManager.getQuery("user_loging.write");
					
					DAO.update(con,queryStr,param_1);
				}
				
				//멀티 체크박스 입력 테스트 종료
			}
			
			listMap.put("loginContent",list);
			logger.debug("queryStr ==> "+"login.query");

		     
		     
// 리퀘스트에 모두 담는다.
			req.setAttribute("list_map",listMap);

	

		}catch (Exception ex) {
			try{
				con.rollback();
			}catch(Exception ee){}
			ex.printStackTrace();
			throw new CommandException(ex.getMessage());
		}finally {
			try{
				if (con!=null) con.setAutoCommit(true);
				if (con!=null) con.close();
			}catch(Exception ee){}
		}
		return next;
	}
	


}
